package es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions;

import es.udc.emarketapp.emarket.model.order.vo.OrderVO;
import es.udc.emarketapp.emarket.model.orderDetails.dao.SQLOrderDetailsDAO;
import es.udc.emarketapp.emarket.model.orderDetails.dao.SQLOrderDetailsDAOFactory;
import es.udc.emarketapp.emarket.model.orderDetails.vo.OrderDetailsVO;
import es.udc.emarketapp.emarket.model.orderLine.dao.SQLOrderLineDAO;
import es.udc.emarketapp.emarket.model.orderLine.dao.SQLOrderLineDAOFactory;
import es.udc.emarketapp.emarket.model.purchase.dao.SQLPurchaseDAO;
import es.udc.emarketapp.emarket.model.purchase.dao.SQLPurchaseDAOFactory;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.vo.OrderDetailsChunkVO;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.vo.OrderResumeChunkVO;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.vo.OrderResumeVO;
import es.udc.emarketapp.emarket.model.purchase.vo.PurchaseVO;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;


import java.sql.Connection;
import java.util.List;

import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.NonTransactionalPlainAction;
import java.util.ArrayList;

public class FindOrdersResumeByShippingPendentAction implements NonTransactionalPlainAction {

    private int startIndex;
    private int count;

    public FindOrdersResumeByShippingPendentAction(int startIndex, int count) {

        this.startIndex = startIndex;
        this.count = count;
    }

    /**
     *
     * @return An instance of <code>OrderDetailsChunkVO</code> objects.
     */
    public Object execute(Connection connection) throws InternalErrorException {

        /*
         * Find count+1 purchases to determine if there exist more products
         * above the specified range.
         */
        SQLOrderDetailsDAO orderDAO = SQLOrderDetailsDAOFactory.getDAO();
        SQLOrderLineDAO orderLineDAO = SQLOrderLineDAOFactory.getDAO();
        SQLPurchaseDAO purchaseDAO = SQLPurchaseDAOFactory.getDAO();
        List<OrderDetailsVO> orderDetailsVOs = orderDAO.findByShippingPendent(connection,
                startIndex, count + 1);
        boolean existMoreOrders = orderDetailsVOs.size() == (count + 1);

        List<OrderResumeVO> orderResumeVOs = new ArrayList<OrderResumeVO>();
        int nUds = 0;

        try {

            for (OrderDetailsVO order : orderDetailsVOs) {

                PurchaseVO purchase = purchaseDAO.findById(connection, order.getnCompra());
                nUds = orderLineDAO.countUdsByOrder(connection, order.getnPedido());

                orderResumeVOs.add(new OrderResumeVO(order.getnPedido(),
                        order.getnCompra(), order.getIdVendedor(),
                        purchase.getIdComprador(), order.getValor() + order.getGastosEnvio(),
                        purchase.getFecha(), nUds, order.getFechaPago(),
                        order.getFechaEnvio()));

            }

        } catch (InstanceNotFoundException e) {
            throw new InternalErrorException(e);
        }

        /*
         * Remove the last account from the returned list if there exist more
         * accounts above the specified range.
         */
        if (existMoreOrders) {
            orderDetailsVOs.remove(orderDetailsVOs.size() - 1);
        }

        return new OrderResumeChunkVO(orderResumeVOs, existMoreOrders);


    }
}
